package com.lucian.ge.controller.shopadmin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.auth.controller.GetAuthController;
import com.lucian.ge.service.shop.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import com.lucian.ge.comman.model.ResultMap;

import java.util.List;
import java.util.Map;

/**
 * Created by Lucicol on 2019/6/19.
 */
@Api(value = "CategoryManageController", description = "shopadmin-商品分类")

@RestController
@RequestMapping("/manage/category")
public class CategoryManageController {

    @Value("${com.authenCenter}")
    private String authenCenter;
    private final ResultMap resultMap;

    @Autowired
    public CategoryManageController(ResultMap resultMap) {
        this.resultMap = resultMap;
    }

    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value="test", notes="测试")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType="header", name = "user", value = "user", required = false, dataType = "String")
    })

    @GetMapping("/test")
    public ResultMap test(@RequestHeader String token, @RequestHeader String user) {

        // 管理员检查
//        String authCheck  = GetAuthController.getAdminAuth(token, user);
//        if (authCheck.equals("301")) {
//            //            return resultMap.success().code(301).message(result).res(JSON.parse(result));
//            return resultMap.success().code(301).message("您没有管理员权限访问！");
//        }
//        if (!authCheck.equals("200")) {
//            //        return resultMap.success().code(200).message(JSON.toJSONString(result)).data(JSON.parseArray(result));
//            //        return resultMap.success().code(200).message(result).res(JSON.parse(result));
//            return resultMap.success().code(400).message("服务错误");
//        }
        System.out.println("ok,管理员浪吧");
        return resultMap.success().code(200).message("测试:管理员成功操作").token("").res("").data(null);
    }

    @GetMapping("/test2")
    public ResultMap test2(@RequestHeader String token, @RequestHeader String user) {

        // 普通用户检查
//        String authCheck  = GetAuthController.getUserAuth(token, user);
//        if (authCheck.equals("301")) {
//            return resultMap.success().code(301).message("您没有普通用户权限访问！");
//        }
//        if (!authCheck.equals("200")) {
//            return resultMap.success().code(400).message("服务错误");
//        }
        System.out.println("ok, 普通用户飞吧");
        return resultMap.success().code(200).message("测试:普通用户成功操作").token("").res("").data(null);
    }

    /**
     * 添加商品分类
     * @param token
     * @return
     */
    @ApiOperation(value="添加商品分类", notes="")

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header", name = "token", value = "token", required = false, dataType = "String")
    })

    @GetMapping("/add_category")
    @ResponseBody
    public ResultMap add_category (@RequestHeader String token) {

        if (!GetAuthController.getAdminAuth(token).equals("管理员验证通过")) {
            return resultMap.success().code(200).message(GetAuthController.getAdminAuth(token)).token("").res("").data(null);
        }

        return resultMap.success().code(200).message("添加分类成功").token("").res("").data(null);

    }

    /**
     * 获得商品分类
     * @param token
     * @return
     */
    @ApiOperation(value="获得商品分类", notes="")
    @GetMapping("/get_category")
    @ResponseBody
    public ResultMap get_category (@RequestHeader String token, @RequestParam(value = "categoryId" ,defaultValue = "0") Integer categoryId) {

        if (!GetAuthController.getAdminAuth(token).equals("管理员验证通过")) {
            return resultMap.success().code(200).message(GetAuthController.getAdminAuth(token)).token("").res("").data(null);
        }

//        CategoryService.getChildrenParallelCategory(categoryId);
        JSONObject.parseObject(JSON.toJSONString(categoryService.findChildCategory(categoryId)));
//        Map<String, String> asdf = categoryService.selectCategoryAndChildrenById(categoryId);

        Map<String, String> category = categoryService.findChildCategory(categoryId);
        System.out.println(categoryService.findChildCategory(categoryId));
        System.out.println(JSONObject.parseObject(JSON.toJSONString(category)));
        return resultMap.success().code(200).message("获得分类成功").token("").res(categoryService.findChildCategory(categoryId)).data(null);

    }

}
